<?php
define("ADMIN",1);
define("_ACCESS",'ADMIN');
define('_LOCATION', 'ADMIN');



global $db, $item, $id, $option;
session_start();
include_once('../configuration.php');
require_once('../includes/DbConnector.php');

$db = new DbConnector();
/*=================*/
//$db->dbg = true;
/*=================*/
$location=$db->getSetting('_absolute_path');
$Lang = $db->getSetting('Lang');
if (!$Lang) require_once($location."/lang/EN.php"); else require_once($location."/lang/$Lang.php");
require_once($location.'includes/kernel.php');

/*=================================================
===================================================
=================================================*/
//$access=='admin';
//$_SESSION['access']='admin';
//$_SESSION['usrname']='admin';
//echo md5('007');
//$uname='admin';
/*=================================================
===================================================
=================================================*/
if (isset( $_POST['submit'] ))
{
	$usrname 	= $_POST['usrname'];
	$pass 		= md5($_POST['pass']);
	if (!$pass)
	{
		echo "<script>alert('Введите пароль');";
		echo "document.location.href='index.php';</script>\n";
		exit();
	}
	$res=$db->query('SELECT * FROM users WHERE username=\''.$usrname.'\' AND password=\''.$pass.'\' LIMIT 0,1');
	if($cur_res =  $db->fetchObject($res))
	{		
		$db->setSetting('usrname', $cur_res->username);
		$db->setSetting('pass', $cur_res->password);	
		$db->setSetting('uname', $cur_res->name);
		$db->setSetting('Filial_id', $cur_res->filial_link);		
		$db->setSetting('User_id', $cur_res->id);
		$db->setSetting('Lang', $cur_res->lang);
		$db->setSetting('ViewDate', date("d.m.Y"));
		$db->setSetting('ViewDateRel', "now");
		
		
		
	
		if ( $cur_res->usertype === "admin" ) 
		{
			$db->setSetting('access', 'admin');
		}
		elseif($cur_res->usertype === "user") 
		{
			$db->setSetting('access', 'user');			
			echo "<script>document.location.href='".$db->getSetting('_live_site')."index.php';</script>\n";
			exit() ; 
		}			
		else $db->setSetting('access', null);		
	}
	else
	{
		echo "<script>alert('Неправильное Имя или Пароль. Попробуйте снова');";
		echo"document.location.href='index.php';</script>\n";
		exit();
	}	
}
else
{
	if ( !isset($_SESSION['usrname']) and !isset($_SESSION['pass']) )
	{
		echo "<script>alert('Чтобы зайти на эту страницу необходимо ввести Имя и Пароль');";
		echo "document.location.href='index.php';</script>\n";
		exit();
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo "Управление сайтом $_site_name"; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script language="JavaScript" src="<?php echo $_live_site.'admin/'?>js/my_mini.js" type="text/javascript"></script>
<style type="text/css">
@import url(style/theme.css);
</style>
</head>
<body>
<br>
<div align="center">
<div class="main">
<table width="100%" border="0">
  <tr>
    <td valign="middle" align="center">
			<table class="adminheading" border="0">
			<tr>
			<th class="cpanel">
			Панель управления
			</th>
			<td align="center" style="height:100px">
				<?php
				echo $db->getSetting('uname');
				?>
				<a href="index2.php?item=logout" style="text-decoration:none;">
				Выйти
			</a>
			</td>	
			</tr>
			</table>

<?php mainFunc(); ?>

</body>	
</HTML>
				
